package uit.nfcmenu.db.adapter;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

	/** The is run before. */
	public static boolean isRunBefore = false;

	/** The context. */
	Context context;

	public DatabaseHelper(Context context) {
		super(context, DbDefines.DATABASE_NAME, null,
				DbDefines.DATABASE_VERSION);
		this.context = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// Create all data tables
		if (!isRunBefore) {

			createMenuTable(db);
			createItemTable(db);
			createOrderTable(db);
			isRunBefore = true;
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
	}

	static void createMenuTable(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE " + DbDefines.MenuModel + " ("
				+ DbDefines.TableMenu.menuId + " INTEGER,"
				+ DbDefines.TableMenu.menuName + " TEXT,"
				+ DbDefines.TableMenu.imageLink + " TEXT,"
				+ DbDefines.TableMenu.description + " TEXT," 
				+ DbDefines.TableMenu.bitmap + " TEXT," 
				+ "PRIMARY KEY (" + DbDefines.TableMenu.menuId + "))");
	}

	static void createItemTable(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE " + DbDefines.ItemModel + " ("
				+ DbDefines.TableItem.itemId + " INTEGER,"
				+ DbDefines.TableItem.itemName + " TEXT,"
				+ DbDefines.TableItem.menuId + " INTEGER,"
				+ DbDefines.TableItem.imageLink + " TEXT,"
				+ DbDefines.TableItem.price + " TEXT,"
				+ DbDefines.TableItem.moneyType + " TEXT,"
				+ DbDefines.TableItem.description + " TEXT,"
				+ DbDefines.TableItem.discount + " TEXT," 
				+ DbDefines.TableItem.bitmap + " TEXT," 
				+ "PRIMARY KEY (" + DbDefines.TableItem.itemId + "))");
	}

	static void createOrderTable(SQLiteDatabase db) {
		String query = "CREATE TABLE " + DbDefines.OrderModel + " ("
				+ DbDefines.TableOrder.orderId + " INTEGER,"
				+ DbDefines.TableOrder.itemId + " INTEGER,"
				+ DbDefines.TableOrder.status + " TEXT,"
				+ DbDefines.TableOrder.tableNo + " INTEGER,"
				+ DbDefines.TableOrder.orderTime + " TEXT,"
				+ DbDefines.TableOrder.orderUser + " TEXT,"
				+ DbDefines.TableOrder.quantity + " INTEGER,"
				+ DbDefines.TableOrder.description + " TEXT,"
				+ DbDefines.TableOrder.orderTimeMilisecond+" BIGINT,"
				+ DbDefines.TableOrder.comment + " TEXT," 
				+ "PRIMARY KEY (" + DbDefines.TableOrder.orderId + "))";
		db.execSQL(query);
	}
}
